package com.yuezhu.constomview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuezhu.global.ApplicationContext;
import com.yuezhu.R;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * Created by zhao on 2016/5/4.
 */
public class TentenView extends LinearLayout {

    @InjectView(R.id.add_apartment)
    TextView addApartment;
    @InjectView(R.id.water_and_electricity)
    TextView waterAndElectricity;
    @InjectView(R.id.signing)
    TextView signing;
    @InjectView(R.id.announcement)
    TextView announcement;
    private GridView tenGrid;
    TextView asingment_text;
    private GridAdapter adapter;
    int screen_width;

    public TentenView(Context context) {
        super(context);
        initView(context);
    }

    public TentenView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TentenView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        screen_width = ApplicationContext.getInstance().getScreen_width();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_tenten_view, this);
        addApartment = (TextView) view.findViewById(R.id.add_apartment);
        waterAndElectricity = (TextView) view.findViewById(R.id.water_and_electricity);
        signing = (TextView) view.findViewById(R.id.signing);
        announcement = (TextView) view.findViewById(R.id.announcement);
        tenGrid = (GridView) view.findViewById(R.id.tenten);
        adapter = new GridAdapter();
        tenGrid.setAdapter(adapter);
        asingment_text = (TextView) view.findViewById(R.id.asingment_text);
        asingment_text.setText("公告");
        setLayoutParame();
    }

    private void setLayoutParame() {
        LayoutParams params = (LayoutParams) asingment_text.getLayoutParams();
        params.width = screen_width - 60;
        params.height = screen_width;
        params.topMargin = 20;
        params.bottomMargin = 30;
    }

    public void setTentenViewClickListener(OnClickListener listener, AdapterView.OnItemClickListener itemClickListener) {
        addApartment.setOnClickListener(listener);
        waterAndElectricity.setOnClickListener(listener);
        signing.setOnClickListener(listener);
        announcement.setOnClickListener(listener);
        tenGrid.setOnItemClickListener(itemClickListener);
    }


    class GridAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.gride_item, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.grideItemText.setText("爱家公寓\n出租：10\n总房数：100");
            return convertView;
        }

        public GridAdapter() {
            super();
        }

         class ViewHolder {
            @InjectView(R.id.gride_item_text)
            TextView grideItemText;

            ViewHolder(View view) {
                ButterKnife.inject(this, view);
            }
        }
    }
}
